home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 August: Tool Chest / Dev.CD Aug 98 TC.toast / Sample Code / Networking / Http Server / •OT_Classes / TCachedStorage.h < prev    next >
Encoding:
Text File  |  1996-01-11  |  3.0 KB  |  107 lines  |  [TEXT/CWIE]

  1. //    TCachedStorage.h - Cached memory  managemnent
  2. // 
  3. // Apple Macintosh Developer Technical Support
  4. // Written by:  Vinne Moscaritolo
  5. //
  6. //  Copyright (work in progress)  Apple Computer, Inc All rights reserved.
  7. //
  8. // You may incorporate this sample code into your applications without
  9. // restriction, though the sample code has been provided "AS IS" and the
  10. // responsibility for its operation is 100% yours.  However, what you are
  11. // not permitted to do is to redistribute the source as "DSC Sample Code"
  12. // after having made changes. If you're going to re-distribute the source,
  13. // we require that you make it clear in the source that the code was
  14. // descended from Apple Sample Code, but that you've made changes.
  15. // 
  16.  
  17. //
  18. //  define your class Tfoo as:
  19. //         class  Tfoo  : public TCachedStorage<Tfoo>
  20. //
  21.  
  22.  
  23. #ifndef _H_CACHEDSTORAGE
  24. #define _H_CACHEDSTORAGE
  25.  
  26. #ifndef VARIABLE_LENGTH_BLOCKS
  27. #define VARIABLE_LENGTH_BLOCKS 0    // Do we support variable length objects..NO NO NO
  28. #endif
  29.  
  30. #include "TList.h"
  31.  
  32. template <class T> class TCachedStorage {
  33.     
  34. public:
  35.      void*     operator new     (size_t size);
  36.      void     operator delete (void* deadObject) { fgCacheList.Enqueue((TLink*) deadObject); };
  37.  
  38. // CLASS VARIABLES
  39. private:
  40.     static     TLifo     fgCacheList;
  41.     static     Boolean fgRegistered;
  42. };
  43.  
  44. template <class T>  TLifo    TCachedStorage<T>::fgCacheList; 
  45. template <class T>  Boolean TCachedStorage<T>::fgRegistered; 
  46.  
  47. // ---------------------------------------------------------------------------
  48. //     TCachedStorage<T>::operator new 
  49. // ---------------------------------------------------------------------------
  50. //    create a TCachedStorage class from cache if possible
  51. // 
  52. //
  53.  
  54.  
  55. template <class T> void* TCachedStorage<T>::operator new(size_t size)
  56. {
  57.     void *newObject;
  58.     
  59.     if(!fgRegistered) fgRegistered++, TStorageManager::Register(&fgCacheList);
  60.     
  61. #if VARIABLE_LENGTH_BLOCKS
  62.      if( size != sizeof( TCachedStorage<T> )  ) return TStorageManager::Allocate(size);  // handle derived classes
  63. #endif        
  64.     if(! (newObject = fgCacheList.Dequeue()) )                                 // if we have one cached  on stack
  65.         if(! (newObject = TStorageManager::Allocate(size)))                 // else attempt to get it from heap
  66.             { TStorageManager::Purge();                                      // try to free up some ram
  67.                 if(! (newObject = TStorageManager::Allocate(size)))            // one last try
  68.                     DebugStr("\p TCachedStorage -- could not allocate "); }    // Lose...
  69.     return newObject;
  70.     }
  71.  
  72.  
  73. //
  74. // TStorageManager - Interrupt safe memory free
  75. //
  76.  
  77. class TStorageManager {
  78. // CLASS METHODS
  79. public:
  80.     static void* Allocate(size_t size)         { return ::OTAllocMem (size); };
  81.     static void  Free(void* deadObject)     { ::OTFreeMem(deadObject); };
  82.     static void  Purge();                    // Purge all Stacks    
  83.  
  84. //private:
  85.     static void  Register(TLifo*);            // register stack to purge
  86.     static void  UnRegister(TLifo*);
  87.  
  88.  
  89. // FINALIZATION CLASS
  90. private:
  91.     class Init
  92.      {
  93.         ~Init();        
  94.         friend class TStorageManager;
  95.     };
  96.  
  97. // CLASS VARIABLES
  98. protected:
  99.     static TList fgPurgeList;                 // List of Stacks to purge
  100.     static TStorageManager::Init fgInit;    // class initialisation object
  101.  
  102. };
  103.  
  104.  
  105. #endif
  106.  
  107.